﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace Jackie.Silverliight.SetpBySetp
{
    public partial class DragAndDrop : Page
    {
        public DragAndDrop()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private bool trackingMouseMove = false;
        private Point mousePoint;

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            mousePoint = e.GetPosition(null);
            trackingMouseMove = true;
            if(element!=null)
            {
                element.CaptureMouse();
                element.Cursor = Cursors.Hand;
            }
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if(trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePoint.Y;
                double deltaH = e.GetPosition(null).X - mousePoint.X;
                double newTop = deltaV + (double) element.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double) element.GetValue(Canvas.LeftProperty);

                element.SetValue(Canvas.TopProperty, newTop);
                element.SetValue(Canvas.LeftProperty, newLeft);

                mousePoint = e.GetPosition(null);
            }
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();
            mousePoint.X = mousePoint.Y = 0;
            element.Cursor = null;
        }

    }
}
